"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectBalanceCtrl
 * @description
 * # ProjectBalanceCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectBalanceCtrl", function(
    $scope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService,
    DataService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    // 选择款项用途
    $scope.chooseProject = function() {
      // 将列表的数据序列化存入缓存以便回来时候使用
      $scope.param.moduleNo = "";
      $scope.param.relatedModule = "project_gathering";
      // 里面包含了title
      var relModuleObj = {
        selectTab: $scope.param.relatedModule,
        param: { projectId: $scope.param.projectId }
      };
      Util.savaDataforsearch(relModuleObj);
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };
    $scope.chooseinfo = function() {
      if (!$scope.param.projectId) {
        Util.Toast("请先选择项目名称");
        return;
      }
      Util.savaParamForReview($scope.param, id);
      $scope.param.relatedModule = "project_gathering";
      // 里面包含了title
      var relModuleObj = {
        selectTab: $scope.param.relatedModule,
        param: { projectId: $scope.param.projectId }
      };
      Util.savaDataforsearch(relModuleObj);
      $location.path("finance/choosepeople");
    };

    $scope.showbalancelist = function() {
      if (!$scope.param.projectId) {
        Util.Toast("请先选择项目名称");
        return;
      }
      DataService.getData(openApi.unclearList, {
        projectId: $scope.param.projectId
      }).then(function(data) {
        if (data) {
          $scope.param.details = data;
        } else {
          Util.Toast("未加载到需要被清账的扣款记录");
        }
      });
    };
    $scope.deleteItem = function(index) {
      $scope.param.details.splice(index, 1);
      $scope.index = -1;
    };
    $scope.swipe = function(index) {
      $scope.index = index;
    };
    $scope.resetlist = function() {
      $scope.index = -1;
    };

    $scope.submit = function(operationId) {
      $scope.param.costIds = $scope.param.details.map(function(item) {
        return item.costId;
      });
      // $scope.param.costIds = angular.toJson($scope.param.costIds);

      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectrepay;
      options.param = angular.copy($scope.param, {});
      delete options.param.details;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }
      if (!$scope.param.details || $scope.param.details.length < 1) {
        Util.Toast("请选择清账明细");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        $scope.param = Object.assign({}, $scope.param, data);
      });
    };
    // 初始化
    init();
  });
